/**
 * 
 */
package com.tomagency.cyberlab.client.activity;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasText;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.ServerFailure;
import com.google.web.bindery.requestfactory.shared.Violation;
import com.tomagency.cyberlab.client.ClientFactory;
import com.tomagency.cyberlab.client.CssGlobalStyleNames;
import com.tomagency.cyberlab.client.events.*;
import com.tomagency.cyberlab.client.place.ObjectListenPlace;
import com.tomagency.cyberlab.client.ui.ObjectListenView;
import com.tomagency.cyberlab.client.ui.ObjectListenView.ObjectEventHandler;
import com.tomagency.cyberlab.client.ui.ObjectListenView.Presenter;


import de.gammadata.gwt.modelle.ResultObject;
import de.gammadata.gwt.proxy.*;


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author gfr
 *
 */
public class BelegListenActivity extends AbstractListenActivity implements Presenter<BelegEvent>,ObjectEventHandler<BelegEvent>{
	
	

	public BelegListenActivity(ObjectListenPlace place,ObjectListenView view,ClientFactory clientFactory) {
		super(place, view, clientFactory);
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.tomagency.cyberlab.client.activity.AbstractListenActivity#holeListeVomServer()
	 */
	@Override
	public void holeListeVomServer(){
		if (isDataFetchinProgress())
			return;
//		searchTxt.setText("");
		getView().setMessage("hole Daten vom Server ......");
//		errorLabel.setText("hole Daten vom Server");
		setDataFetchinProgress(true);
		
		getClientFactory().getRpcServiceProvider().getRequestFactory().belegRequest().findAllBelege(getStart(),getLimit()).fire(
				new Receiver<List>(){

					@Override
					public void onSuccess(List response) {	
						setDataFetchinProgress(false);
						onListRequestCompleted(response);
						
					}


					@Override
					public void onFailure(ServerFailure error) {
						setDataFetchinProgress(false);
						super.onFailure(error);
					}

					@Override
					public void onViolation(Set<Violation> errors) {
						setDataFetchinProgress(false);
						super.onViolation(errors);
					}
					
				}
				);
	}



	/* (non-Javadoc)
	 * @see com.tomagency.cyberlab.client.activity.AbstractListenActivity#sucheBelege(java.lang.String)
	 */
	@Override
	public void sucheObjekte(String suchText){
		if (isDataFetchinProgress())
			return;
		setDataFetchinProgress(true);
		setLastSuchText(suchText);
		getView().setMessage("suche nach: "+suchText+", hole Daten vom Server");
		getClientFactory().getRpcServiceProvider().getRequestFactory().belegRequest().findByMatchString(suchText).fire(
				new Receiver<List>(){

					@Override
					public void onSuccess(List response) {	
						
						onSearchRequestCompleted(response);
						setDataFetchinProgress(false);
					}
					
					@Override
					public void onFailure(ServerFailure error) {
						setDataFetchinProgress(false);
						super.onFailure(error);
					}

					@Override
					public void onViolation(Set<Violation> errors) {
						setDataFetchinProgress(false);
						super.onViolation(errors);
					}
					
				}
				);
	}
	
	/* (non-Javadoc)
	 * @see com.tomagency.cyberlab.client.activity.AbstractListenActivity#onObjectClicked(int, java.lang.Object)
	 */
	@Override
	public void onObjectClicked(int rowNr, int objectId) {
		setCurrentRowNr(rowNr);
//		BelegListProxy beleg = (BelegListProxy)clickedObject;
//		Window.alert("Zeile: "+rowNr+" Beleg: "+beleg.getNummer());		
		getClientFactory().getRpcServiceProvider().getRequestFactory().belegRequest().findBelegExt(objectId).with("adresse","kontakt","kontakt.kommAdressenListe","emailAdresse","versandArt","zahlungsBedingung","positionsListe","messageListe").fire(
					new Receiver<BelegViewProxy>(){

						@Override
						public void onSuccess(BelegViewProxy beleg) {
							onObjectRequestCompleted(beleg,getCurrentRowNr());
//							renderListRow(beleg,selectedRow,RenderType.FULL);
//							Window.alert("Beleg geholt: "+beleg.getUuid()+ ", "+beleg.getSubject());

						}

						/* (non-Javadoc)
						 * @see com.google.web.bindery.requestfactory.shared.Receiver#onFailure(com.google.web.bindery.requestfactory.shared.ServerFailure)
						 */
						@Override
						public void onFailure(ServerFailure error) {
							super.onFailure(error);
							Window.alert("Fehler beim Laden des Objekts: "+error.getMessage()+"\n"+error.getMessage());
						}

						/* (non-Javadoc)
						 * @see com.google.web.bindery.requestfactory.shared.Receiver#onViolation(java.util.Set)
						 */
						@Override
						public void onViolation(Set<Violation> errors) {
							super.onViolation(errors);
						}

						
					}
			);	
	}



	
	@Override
	public void onObjectEvent(BelegEvent event) {
		
		onObjectEventStarted(event);
		setCurrentObjectId(event.getObjectId());
		setCurrentRowNrByObjectId(event.getObjectId());
		switch(event.getEventType()){
		
		
		case ObjectEvent.ONINVOKE_FUNCTION:
			
			if (event.getFunctionCode()==BelegEvent.FUNCTION_GEN_BUBU_XML)
//				Window.alert("BelegEvent FUNCTION_GEN_BUBU_XML objId: "+event.getObjectId());
				getClientFactory().getRpcServiceProvider().getBelegService().placeBuBuOrder(event.getObjectId(), new AsyncCallback<ResultObject>(){
					@Override
					public void onFailure(Throwable e) {
						onObjectEventCompleted("  Fehler"+e.getMessage(),false);
//						getView().setMessage("  Fehler"+e.getMessage());						
					}

					@Override
					public void onSuccess(ResultObject result) {
						if (result.isOk()){
							onObjectClicked(getCurrentRowNr(),getCurrentObjectId());
							onObjectEventCompleted(result.getMessage(),result.isOk());
						}
						else{
							onObjectEventCompleted(result.getMessage(),result.isOk());
						}
//						getView().setMessage("  "+result.getMessage());
					}
					
				});
				
			break;
			
		case ObjectEvent.ONSETPROPERTIES:
			if (event.getProperties()!=null&&event.getProperties().containsKey("status")&&event.getObjectId()>0){
				/*
				 * Status auf dem Server rückschreiben
				 */
				/*
				getClientFactory().getRpcServiceProvider().getBelegService().setBelegStatus(event.getObjectId(),event.getProperties().get("status"), new AsyncCallback<ResultObject>(){
					@Override
					public void onFailure(Throwable e) {
						onObjectEventCompleted("  Fehler"+e.getMessage(),false);
//						getView().setMessage("  Fehler"+e.getMessage());					
					}

					@Override
					public void onSuccess(ResultObject result) {
						onObjectEventCompleted(result.getMessage(),result.isOk());
//						onObjectEventCompleted(result.getMessage(),result.isOk());
//						getView().setMessage("  "+result.getMessage());
					}
					
				});
				*/
				
				getClientFactory().getRpcServiceProvider().getRequestFactory().belegRequest().setBelegStatusNeu(event.getObjectId(),event.getProperties().get("status"))
				.with("adresse","kontakt","emailAdresse","versandArt","zahlungsBedingung","positionsListe","messageListe").fire(
						new Receiver<BelegViewProxy>(){

							@Override
							public void onSuccess(BelegViewProxy beleg) {
								onObjectRequestCompleted(beleg,getCurrentRowNr());
//								renderListRow(beleg,selectedRow,RenderType.FULL);
//								Window.alert("Beleg geholt: "+beleg.getUuid()+ ", "+beleg.getSubject());

							}

							/* (non-Javadoc)
							 * @see com.google.web.bindery.requestfactory.shared.Receiver#onFailure(com.google.web.bindery.requestfactory.shared.ServerFailure)
							 */
							@Override
							public void onFailure(ServerFailure error) {
								super.onFailure(error);
								Window.alert("Fehler beim Laden des Objekts: "+error.getMessage()+"\n"+error.getMessage());
							}

							/* (non-Javadoc)
							 * @see com.google.web.bindery.requestfactory.shared.Receiver#onViolation(java.util.Set)
							 */
							@Override
							public void onViolation(Set<Violation> errors) {
								super.onViolation(errors);
							}

							
						}
				);	
				
			}
			break;		
		}
	}
	

	@Override
	public void setPresenterInView(ObjectListenView view) {
		view.setPresenter(this);
		view.setObjectEventHandler(this);
		
	}







	
}//Ende der Classe

